package cn.jishiyu11.xeqb.utils.PublicClass

import android.app.Activity
import android.content.DialogInterface
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.Button

import java.io.Serializable
import java.util.ArrayList

import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.utils.AppUtil
import cn.jishiyu11.xeqb.utils.SharedPreferencesUtils
import cn.jishiyu11.xeqb.utils.StringUtil
import cn.jishiyu11.xeqb.view.BottomDialog
import cn.jishiyu11.xeqb.view.PublicDialog

/**
 * Created by vvguoliang on 2017/6/30.
 * 公共类 Diaog
 */

class ShowDialog
/**
 * private的构造函数用于避免外界直接使用new来实例化对象
 */
private constructor() : Serializable {

    /**
     * 单例对象实例
     */
    private object ShowDialogHolder {
        internal val INSTANCE = ShowDialog()
    }

    /**
     * readResolve方法应对单例对象被序列化时候
     */
    private fun readResolve(): Any {
        return instance
    }

    /**
     * @author jsy_zj
     * created at 2017/11/20 15:58
     * 方法说明：创建dialog 单选dialog列表项
     */
    fun getDialog(mActivity: Activity, list: MutableList<Map<String, Any>>, name: String, mHandler: Handler, what: Int) {
        var list = list
        val builder = PublicDialog.Builder(mActivity)
        val stringa = SharedPreferencesUtils[mActivity, name, ""].toString()
        if (!StringUtil.isNullOrEmpty(stringa)) {
            val listViewEntities = SharedPreferencesUtils.getInfo(mActivity, stringa)
            if (null != listViewEntities && listViewEntities.size > 0) {
                list.clear()
                list = listViewEntities
            }
        }
        val finalList = list
        builder.setItems(list, name) { dialog, which ->
            var i = 0
            while (finalList.size > i) {
                if (finalList[i]["boolean"] == "2") {
                    val message = Message()
                    message.what = what
                    if (!finalList[i].toString().contains("number")) {
                        message.obj = finalList[i]["name"].toString()
                    } else {
                        message.obj = finalList[i]["name"].toString() + "," + finalList[i]["number"].toString()
                    }
                    mHandler.dispatchMessage(message)
                }
                i++
            }
            dialog.dismiss()
        }
        builder.create().show()
    }

    companion object {

        val instance: ShowDialog
            get() = ShowDialogHolder.INSTANCE
    }

}
